<?php
namespace app\api\controller;
use \think\Db;
use \think\Model;
use think\Session;
use Yurun\OAuthLogin\QQ\OAuth2;

class User extends Model
{
    protected $resultSetType = 'collection';
    protected $createTime = false;
    protected $updateTime = false;
}
class Url extends Model
{
    protected $resultSetType = 'collection';
    protected $createTime = false;
    protected $updateTime = false;
}
class Api extends \think\Controller
{
    public function qq_api()    //  qq登录接口
    {
        $qqauth = new \Yurun\OAuthLogin\QQ\OAuth2('101932555', 'bedc044973e286c18feaf8c4f04e4465', 'http://localhost/api/Api/callback');
        $url = $qqauth->getAuthUrl();
        Session::set('YURUN_QQ_STATE', $qqauth->state);
        $this->redirect($url);
    }
    public function callback()  //QQ登录回调地址
    {
        $qqauth = new \Yurun\OAuthLogin\QQ\OAuth2('101932555', 'bedc044973e286c18feaf8c4f04e4465', 'http://localhost/api/Api/callback');
        $accessToken = $qqauth->getAccessToken(Session::get('YURUN_QQ_STATE'));
        $userInfo = $qqauth->getUserInfo();
        $openid = $qqauth->openid;
        $nickname = $userInfo['nickname'];
        $gender = $userInfo['gender'];
        $local = $userInfo['province'] . $userInfo['city'];
        $birth_year = $userInfo['year'];
        $user = Db::name('user')->where('qq_sid', $openid)->find();
        $rand_code = random(20, 'all');
        if (!empty($user)) {
            Db::name('user')->where('qq_sid', $openid)->update(['safecode' => $rand_code]);
            cookie(['prefix' => '', 'expire' => 36000]);
            cookie('safecode', $rand_code, '36000');
            $this->success('登陆成功', 'home/Home/home');
        } else {
            Session::set('qq_sid', $openid);
            Session::set('nickname', $nickname);
            Session::set('gender', $gender);
            Session::set('local', $local);
            Session::set('birth_year', $birth_year);
            $this->redirect('index/Bind/bind');
        }
    }
    public function api()
    {
        $get_data = input('get.');
        if (!$get_data['url'] or !$get_data['code']) {
            $message = [
                'message' => 'error'
            ];
            return json($message);
        }
        $user = User::getbyCode($get_data['code'])->toArray();
        $uid = $user['Id'];
        $domain = $get_data['url'];
        $url = Url::getByDomain($get_data['url']);   //从数据库查找有无此域名存在
        if ($url) {
            $message = [
                'message' => 'error,存在此域名'
            ];
            return json($message);
        }
        $identity_code = random(32, 'string', 0);
        $web_data = new Url;
        $web_data->data([
            'uid' => $uid,
            'domain' => $domain,
            'identity_code' => $identity_code
        ]);
        if ($web_data->save()) {
            $message = [
                'message' => 'success',
                'identity_code' => $identity_code
            ];
            return json($message);
        }
    }
}
